<?php
	class Manage_user extends CI_Controller{
		public function __construct(){
			parent::__construct();
			$this->load->library('session');
			$this->load->library('form_validation');
			$this->load->library('cismarty');
			$this->load->model("apanel/template");
			$this->load->model("apanel/db_user");
			$this->load->model("apanel/db_news");
			$this->load->model("apanel/db_info");
			$this->load->library('pagination');
		}
		
		public function index(){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			redirect("manage_user/user_list");
		}
		
		public function user_list($st=""){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			list($cat,$num,$page,$total) = $this->db_user->user_all();
			$this->cismarty->assign("Num_Rows",$num);
			$this->cismarty->assign("cat",$cat);
			$this->cismarty->assign("pagelink",$page);
			$this->cismarty->assign("total",$total);
			if($st == "success"){
				$this->cismarty->assign("status","<div class=\"mws-form-message success message\">Success</div>");
			}
		
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(9));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open('manage_user/user_delAll'));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/user/user_list.html');
		}
		
		public function user_add(){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($this->input->post('bntSubmit') != null){
				$this->form_validation->set_rules('user', 'user', 'trim|required|min_length[5]|max_length[15]|callback_username_check');
				$this->form_validation->set_rules('pass', 'Password', 'required|matches[cpass]');
				$this->form_validation->set_rules("customer","Customer","required");
				$this->form_validation->set_rules('cpass', 'Confirm Password', 'required');
				$this->form_validation->set_rules('access', 'User Access', 'required');
				$this->form_validation->set_rules('name', 'Name Surname', 'required');
				$this->form_validation->set_rules("email","email","trim|valid_email");
				$this->form_validation->set_rules("role","Role","trim");
				if($this->form_validation->run() == FALSE){
					$this->cismarty->assign("customer",set_value("customer"));
					$this->cismarty->assign("name",set_value('name'));
					$this->cismarty->assign("user",set_value('user'));
					$this->cismarty->assign("email",set_value('email'));
					$this->cismarty->assign("role",set_value('role'));
						
					//set error
					$this->cismarty->assign("errorusername",form_error('user'));
					$this->cismarty->assign("errorpass",form_error('pass'));
					$this->cismarty->assign("errorcpass",form_error('cpass'));
					$this->cismarty->assign("errorname",form_error('name'));
					$this->cismarty->assign("erroremail",form_error('email'));
					$this->cismarty->assign("erroraccess",form_error('access'));
					$this->cismarty->assign("errorcustomer",form_error('customer'));
				}else {
					$data = array(
							"username"=>trim($this->input->post('user')),
							"role_pkid"=>$this->input->post("access"),
							"customer_pkid"=>$this->input->post("customer"),
							"country_pkid"=>$this->input->post('country'),
							"password"=>md5($this->input->post('pass')),
							"name"=>trim($this->input->post('name')),
							"role"=>trim($this->input->post('role')),
							"email"=>trim($this->input->post('email')),
							"created_by"=>$this->session->userdata("sess_username"),
							"created_date"=>date("Y-m-d H:i:s")
					);
					$res = $this->db->insert('mobile_user',$data);
					if($res) redirect('manage_user/user_list/success');
					else  redirect('manage_user/user_list/fail');
				}
			
			}
			$this->cismarty->assign("customer",$this->db_news->customer_list());
			$this->load->model("apanel/db_hotel");
			$this->cismarty->assign("str_country",$this->db_user->country_list());
			$this->cismarty->assign("access",$this->db_user->access_list());
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(9));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open('manage_user/user_add'));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/user/user_add.html');
		}
		
		public function username_check($str){
			$sql = "select * from mobile_user where username =?";
			$res = $this->db->query($sql,array($str));
			$num = $res->num_rows();
			if($num >0 ){
				$this->form_validation->set_message('username_check', 'Duplicate username');
				return false;
			}else {
				return true;
			}
		}
		
		public function user_del($id){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($id != ""){
				$this->db->where('pkid',$id);
				$res = $this->db->delete("mobile_user");
				if($res) redirect('manage_user/user_list/success');
			}
		}
		
		public function user_delAll(){
			$check = $this->input->post('check');
			if($check != ""){
				foreach ($check as $index){
					$this->db->where('pkid',$index);
					$res = $this->db->delete("hotel_user");
				}
			}
			redirect('manage_user/user_list/success');
		}
		
		public function user_edit($id){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			$country = "";
			$level  ="";
			$group = "";
			if($id != ""){
				$this->db->where("pkid",$id);
				$res = $this->db->get('mobile_user');
				$row = $res->row();
				$this->cismarty->assign("user",$row->username);
			}
			if($this->input->post('bntSubmit') != null){
				$this->form_validation->set_rules('pass', 'Password', 'required|matches[cpass]');
				$this->form_validation->set_rules('cpass', 'Confirm Password', 'required');
				$this->form_validation->set_rules('name', 'Name Surname', 'required');
			//	$this->form_validation->set_rules("department","Department","required");
				$this->form_validation->set_rules("email","email","trim|valid_email");
				$this->form_validation->set_rules("role","Role","trim");
				if($this->form_validation->run() == FALSE){
					$this->cismarty->assign("name",set_value('name'));
					//$this->cismarty->assign("user",set_value('user'));
					$this->cismarty->assign("email",set_value('email'));
					$this->cismarty->assign("role",set_value('role'));
			
					//set error
				//	$this->cismarty->assign("errorusername",form_error('user'));
					$this->cismarty->assign("errorpass",form_error('pass'));
					$this->cismarty->assign("errorcpass",form_error('cpass'));
					$this->cismarty->assign("errorname",form_error('name'));
					$this->cismarty->assign("erroremail",form_error('email'));
					//$this->cismarty->assign("errordepartment",form_error('department'));
						
				}else {
					$data = array(
							"customer_pkid"=>$this->input->post('customer'),
							"country_pkid"=>$this->input->post('country'),
							"password"=>md5($this->input->post('pass')),
							"name"=>trim($this->input->post('name')),
							"role"=>trim($this->input->post('role')),
							"email"=>trim($this->input->post('email')),
							"updated_by"=>$this->session->userdata("sess_username"),
							"updated_date"=>date("Y-m-d H:i:s")
					);
					$this->db->where("pkid",$id);
					$res = $this->db->update('mobile_user',$data);
					if($res) redirect('manage_user/user_list/success');
					else  redirect('manage_user/user_list/fail');
				}
					
			}else{
				$this->db->where("pkid",$id);
				$res = $this->db->get('mobile_user');
				$row = $res->row();
				$this->cismarty->assign("name",$row->name);
				$this->cismarty->assign("user",$row->username);
				$this->cismarty->assign("email",$row->email);
				$this->cismarty->assign("role",$row->role);
				$group = $row->customer_pkid;
				$country	= $row->country_pkid;
				$level =$row->role_pkid;
			}
			$this->cismarty->assign("str_country",$this->db_info->country_list($country));
			$this->cismarty->assign("access",$this->db_user->access_list($level));
			$this->cismarty->assign("customer",$this->db_news->customer_list($group));
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(9));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open("manage_user/user_edit/$id"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/user/user_edit.html');
		}
		
		public function profile($st = ""){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			$country ="";
			if($st == "success"){
				$this->cismarty->assign("status","<div class=\"mws-form-message success message\">Success</div>");
			}
			$this->db->where("pkid",$this->session->userdata('sess_userid'));
			$res = $this->db->get('mobile_user');
			$row = $res->row();
			$this->cismarty->assign("user",$row->username);
			
			if($this->input->post('bntSubmit') != null){
				$this->form_validation->set_rules('pass', 'Password', 'required|matches[cpass]');
				$this->form_validation->set_rules('cpass', 'Confirm Password', 'required');
				$this->form_validation->set_rules('name', 'Name Surname', 'required');
				$this->form_validation->set_rules("email","email","trim|valid_email");
				$this->form_validation->set_rules("role","Role","trim");
				if($this->form_validation->run() == FALSE){
					$this->cismarty->assign("name",set_value('name'));
					//$this->cismarty->assign("user",set_value('user'));
					$this->cismarty->assign("email",set_value('email'));
					$this->cismarty->assign("role",set_value('role'));
						
					//set error
					$this->cismarty->assign("errorusername",form_error('user'));
					$this->cismarty->assign("errorpass",form_error('pass'));
					$this->cismarty->assign("errorcpass",form_error('cpass'));
					$this->cismarty->assign("errorname",form_error('name'));
					$this->cismarty->assign("erroremail",form_error('email'));
			
				}else {
					$data = array(
							"country_pkid"=>$this->input->post('country'),
							"password"=>md5($this->input->post('pass')),
							"name"=>trim($this->input->post('name')),
							"role"=>trim($this->input->post('role')),
							"email"=>trim($this->input->post('email')),
							"updated_by"=>$this->session->userdata("sess_username"),
							"updated_date"=>date("Y-m-d H:i:s")
					);
					$this->db->where("pkid",$this->session->userdata('sess_userid'));
					$res = $this->db->update('mobile_user',$data);
					if($res) redirect('manage_user/profile/success');
					else  redirect('manage_user/profile/fail');
				}
					
			}else{
				$this->db->where("pkid",$this->session->userdata('sess_userid'));
				$res = $this->db->get('mobile_user');
				$row = $res->row();
				$this->cismarty->assign("name",$row->name);
				$this->cismarty->assign("user",$row->username);
				$this->cismarty->assign("email",$row->email);
				$this->cismarty->assign("role",$row->role);
				$country	= $row->country_pkid;
			}
			$this->cismarty->assign("str_country",$this->db_info->country_list($country));
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu());
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open("manage_user/profile"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/user/profile.html');
		}
		
		public function user_access($st=""){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			
			list($cat,$num,$page,$total) = $this->db_user->access_all();
			$this->cismarty->assign("Num_Rows",$num);
			$this->cismarty->assign("cat",$cat);
			$this->cismarty->assign("pagelink",$page);
			$this->cismarty->assign("total",$total);
			if($st == "success"){
				$this->cismarty->assign("status","<div class=\"mws-form-message success message\">Success</div>");
			}
			
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(9,2));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open("manage_user/access_delAll/"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/user/user_access.html');
		}
		
		public function access_add(){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($this->input->post('bntSubmit') != null){
				$this->form_validation->set_rules('name', 'Name', 'required');
				$this->form_validation->set_rules("department","Department","required");
				$this->form_validation->set_rules("access","access","trim");
				if($this->form_validation->run() == FALSE){
					$this->cismarty->assign("name",set_value('name'));
					$this->cismarty->assign("access",set_value('access'));
			
					//set error
					$this->cismarty->assign("errorname",form_error('name'));
					$this->cismarty->assign("errordepartment",form_error('department'));
						
				}else {
					$data = array(	
							"name"=>trim($this->input->post('name')),
							"department_pkid"=>$this->input->post('department'),
							"access"=>trim($this->input->post('access'))
					);
					$res = $this->db->insert('hotel_user_access',$data);
					if($res) redirect('manage_user/user_access/success');
					else  redirect('manage_user/user_access/fail');
				}
					
			}
			$this->cismarty->assign("str_dp",$this->db_user->department());
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(9,2));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open("manage_user/access_add"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/user/access_add.html');
		}
		
		public function access_edit($id){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($this->input->post('bntSubmit') != null){
				$this->form_validation->set_rules('name', 'Name', 'required');
				//$this->form_validation->set_rules("department","Department","required");
				$this->form_validation->set_rules("access","access","trim");
				if($this->form_validation->run() == FALSE){
					$this->cismarty->assign("name",set_value('name'));
					$this->cismarty->assign("access",set_value('access'));
						
					//set error
					$this->cismarty->assign("errorname",form_error('name'));
					//$this->cismarty->assign("errordepartment",form_error('department'));
			
				}else {
					$data = array(
							"name"=>trim($this->input->post('name')),
						//	"department_pkid"=>$this->input->post('department'),
							"access"=>trim($this->input->post('access'))
					);
					$this->db->where("pkid",$id);
					$res = $this->db->update('hotel_user_access',$data);
					if($res) redirect('manage_user/user_access/success');
					else  redirect('manage_user/user_access/fail');
				}
					
			}else {
				$this->db->where("pkid",$id);
				$res = $this->db->get('hotel_user_access');
				$row = $res->row();
				$this->cismarty->assign("name",$row->name);
				$this->cismarty->assign("access",$row->access);
				$dname = $row->department_pkid;
			}
			$this->cismarty->assign("str_dp",$this->db_user->department($dname));
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(9,2));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open("manage_user/access_edit/$id"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/user/access_edit.html');
		}
		
		public function access_del($id){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($id != ""){
				$this->db->where('pkid',$id);
				$res = $this->db->delete("hotel_user_access");
				if($res) redirect('manage_user/user_access/success');
			}
		}
		
		public function access_delAll(){
			$check = $this->input->post('check');
			if($check != ""){
				foreach ($check as $index){
					$this->db->where('pkid',$index);
					$res = $this->db->delete("hotel_user_access");
				}
			}
			redirect('manage_user/user_access/success');
		}
		
	}

?>